#include "WeeklySchedule.h"

void processSetAnAppointment(WeeklySchedule&);
void processRemoveAnAppointment(WeeklySchedule&);
void processFindAnAppointment(WeeklySchedule&);
void processMoveAnAppointment(WeeklySchedule&);
void mainLoop(){
	bool cont	= true;			// trigger to stop loop
	unsigned int c;

	WeeklySchedule schedule = WeeklySchedule(); 
	while(cont)
	{
		cout << "	0: print schedule" 	<< endl
			 << "	1: set an appointment" 	<< endl
			 << "	2: remove an appointment" << endl
			 << "	3: find an appointment" 	<< endl
			 << "	4: move an appointment from one time to the next" 	<< endl
			 << "Enter your choice :" << endl;
		cin >> c;
		switch (c) 
		{
			case 0:
				schedule.printSchedule();
				break;
			case 1:
				processSetAnAppointment(schedule);
				break;
			case 2:
				processRemoveAnAppointment(schedule);
				break;
			case 3:
				processFindAnAppointment(schedule);
				break;
			case 4:
				processMoveAnAppointment(schedule);
				break;
			default: 
				cont = false;
		}
	}
}

void processSetAnAppointment(WeeklySchedule& schedule)
{	
	Time start = Time(0,0), end = Time(0,0);
	int day, hour, minute;
	unsigned int c;
	cout << "Enter meeting starting hour: " 	<< endl;
	cin >> c;
	hour = c;
	cout << "Enter meeting starting minutes: " 	<< endl;
	cin >> c;
	minute = c;
	try{
		start = Time(hour,minute);
	} catch (int) {
		cout << "couldn't set start time" 	<< endl;
		return;
	}
	cout << "Enter meeting ending hour: " 	<< endl;
	cin >> c;
	hour = c;
	cout << "Enter meeting ending minutes: " 	<< endl;
	cin >> c;
	minute = c;
	try {
		end = Time(hour,minute);
	} catch (int) {
		cout << "couldn't set ending time" 	<< endl;
		return;	
	}
	cout << "Enter meeting day: " 	<< endl;
	cin >> c;
	day = c;
	try {
		schedule.setAnAppointment(start, end, day);
	} catch (string str) {
		cout << str	<< endl;
	}
	return;
}
void processRemoveAnAppointment(WeeklySchedule& schedule)
{

	Time start = Time(0,0), end = Time(0,0);
	int day, hour, minute;
	unsigned int c;
	cout << "Enter meeting starting hour: " 	<< endl;
	cin >> c;
	hour = c;
	cout << "Enter meeting starting minutes: " 	<< endl;
	cin >> c;
	minute = c;
	try{
		start = Time(hour,minute);
	} catch (int) {
		cout << "couldn't remove start time" 	<< endl;
		return;
	}
	cout << "Enter meeting day: " 	<< endl;
	cin >> c;
	day = c;
	try {
		Appointment* TMPAPP = schedule.removeAnAppointment(start, day);
		if (TMPAPP != 0x0) {
			cout << "Subject: "	<< TMPAPP->getSubject() << endl;
			cout << "End Time: "<< TMPAPP->getEndTime().getHour()<< ":" <<  TMPAPP->getEndTime().getMinutes()<< endl;
		} else {
			cout << "couldn't remove meeting" << endl;
		}
	} catch (int) {
		cout << "couldn't remove meeting" 	<< endl;
	}
	return;
}
void processFindAnAppointment(WeeklySchedule& schedule)
{
	Time start = Time(0,0), end = Time(0,0);
	int day, hour, minute;
	unsigned int c;
	cout << "Enter meeting starting hour: " 	<< endl;
	cin >> c;
	hour = c;
	cout << "Enter meeting starting minutes: " 	<< endl;
	cin >> c;
	minute = c;
	try{
		start = Time(hour,minute);
	} catch (int) {
		cout << "couldn't remove start time" 	<< endl;
		return;
	}
	cout << "Enter meeting day: " 	<< endl;
	cin >> c;
	day = c;
	try {	
		Appointment* TMPAPP = schedule.findAppointment(start, day);
		if (TMPAPP != 0x0) {
			cout << "Subject: "	<< TMPAPP->getSubject() << endl;
			cout << "End Time: "	<< TMPAPP->getEndTime().getHour()<< ":" <<  TMPAPP->getEndTime().getMinutes()<< endl;
		} else {
			cout << "couldn't find meeting" << endl;
		}
	} catch (int) {
		cout << "couldn't find meeting" 	<< endl;	
	}
	return;
}

void processMoveAnAppointment(WeeklySchedule& schedule)
{
	Time startOrig = Time(0,0) , startNew = Time(0,0), endNew = Time(0,0);
	int dayOrig, dayNew , hour, minute;
	unsigned int c;
	cout << "Enter meeting original starting hour: " 	<< endl;
	cin >> c;
	hour = c;
	cout << "Enter meeting original starting minutes: " 	<< endl;
	cin >> c;
	minute = c;
	try{
		startOrig = Time(hour,minute);
	} catch (int) {
		cout << "Couldn't set start time" 	<< endl;
		return;
	}
	cout << "Enter meeting original day: " 	<< endl;
	cin >> c;
	dayOrig = c;
	
	//////////////////////////////////////////////////////////////
	cout << "Enter meeting new starting hour: " 	<< endl;
	cin >> c;
	hour = c;
	cout << "Enter meeting new starting minutes: " 	<< endl;
	cin >> c;
	minute = c;
	try{
		startNew = Time(hour,minute);
	} catch (int) {
		cout << "Couldn't set start time" 	<< endl;
		return;
	}

	/////////////////////////////////////////////////////////////
	cout << "Enter meeting new ending hour: " 	<< endl;
	cin >> c;
	hour = c;
	cout << "Enter meeting new ending minutes: " 	<< endl;
	cin >> c;
	minute = c;
	try{
		endNew = Time(hour,minute);
	} catch (int) {
		cout << "Couldn't set ending time" 	<< endl;
		return;
	} 
	/////////////////////////////
	cout << "Enter new meeting day: " 	<< endl;
	cin >> c;
	dayNew = c;
	

	try {
		if(schedule.moveAppointment(startOrig, dayOrig, startNew, endNew, dayNew)) 
		{
			cout << "Moved" << endl;
		} else {
			cout << "couldn't move meeting" << endl;
		}
	} catch (...) {

		cout << "couldn't move meeting" 	<< endl;
	}
	return;
}

int main(int argc, char** argv)
{
	mainLoop();
	return 0;
}
